var currUser = {};
var userId = $("input[name=id]").val();
if (userId && userId !== '') {
    $.get("user/getById/" + userId, function (user) {
        if (user) {
            $("#loginName").val(user.loginName);
            $("#mobile").val(user.mobile);
            $("#name").val(user.name);
            $("#idNumber").val(user.idNumber);
            currUser = user;
        }
    });
} else {
    $("#createUserMode").removeClass("hidden");
}

var defaultPwd = function () {
    defaultPwdTips = $("#defaultPwdTips");
    defaultPwdTips.text("默认密码（勾选则默认密码为:changeit，否则随机密码将以短信的形式发送至手机mobile）");
    defaultPwdTips.text(defaultPwdTips.text().replace("mobile", $("#mobile").val()));
};

/**
 * 初始化表单验证
 * 
 * @param {type} param
 */
$("#userForm").validate({
    //错误类名和元素标签类型
    errorClass: "gg-error",
    validClass: "gg-success",
    errorElement: "label",
    //验证规则正则表达式
    rules: {
        loginName: {
            required: true,
            regx: /^[a-zA-Z0-9_\u4e00-\u9fb9]{2,30}$/,
            remote: "user/isLoginNameAvailable?&id=" + userId
        },
        mobile: {
            required: true,
            regx: /^1\d{10}$/,
            remote: "user/isMobileAvailable?id=" + userId
        },
        name: {
//            required: true,
//            regx: /^[\u4e00-\u9fa5]{2,10}$/
            regx: /(^[\u4e00-\u9fa5]{2,10}$)|(^\s*$)/
        },
        idNumber: {
//            required: true,
//            regx: /^[1-9][0-9]{16}[0-9xX]$/,
            regx: /(^[1-9][0-9]{16}[0-9xX]$)|(^[1-9][0-9]{14}$)|(^\s*$)/,
            remote: "user/isIdNumberAvailable?id=" + userId
        }
    },
    //验证结果文字提示
    messages: {
        loginName: {
            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        },
        mobile: {
            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        },
        name: {
//            required: '必填',
            regx: '格式不正确'
        },
        idNumber: {
//            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        }
    },
    //验证结果处理
    highlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
    },
    //错误信息位置
    errorPlacement: function (error, element) {
        error.insertAfter(element);
    }
});

/**
 * 提交表单
 * 
 * @param {type} param
 */
$("#saveUserBtn").click(function () {
    if ($("#userForm").valid()) {
        var data = $("#userForm").formToJson();
        data.loginName = $("#loginName").val();
        data.isDefaultPwd = $("#isDefaultPwd").prop("checked");
        $.post("user/save", data, function (res) {
            alert(res.message);
            if (res.success) {
                location.href = "user/list";
            }
        }).fail(function () {
            alert("网络通信错误");
        });
    }
});